package com.ruoyi.admin.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.admin.mapper.AlipayWithdrawalRecordMapper;
import com.ruoyi.admin.domain.AlipayWithdrawalRecord;
import com.ruoyi.admin.service.IAlipayWithdrawalRecordService;
import com.ruoyi.common.utils.DateUtils;

/**
 * 提现记录Service业务层处理
 * 
 * @author system
 * @date 2025-06-26
 */
@Service
public class AlipayWithdrawalRecordServiceImpl implements IAlipayWithdrawalRecordService
{
    @Autowired
    private AlipayWithdrawalRecordMapper alipayWithdrawalRecordMapper;

    /**
     * 查询提现记录
     * 
     * @param id 提现记录主键
     * @return 提现记录
     */
    @Override
    public AlipayWithdrawalRecord selectAlipayWithdrawalRecordById(Long id)
    {
        return alipayWithdrawalRecordMapper.selectAlipayWithdrawalRecordById(id);
    }

    /**
     * 查询提现记录列表
     * 
     * @param alipayWithdrawalRecord 提现记录
     * @return 提现记录集合
     */
    @Override
    public List<AlipayWithdrawalRecord> selectAlipayWithdrawalRecordList(AlipayWithdrawalRecord alipayWithdrawalRecord)
    {
        return alipayWithdrawalRecordMapper.selectAlipayWithdrawalRecordList(alipayWithdrawalRecord);
    }

    /**
     * 新增提现记录
     * 
     * @param alipayWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int insertAlipayWithdrawalRecord(AlipayWithdrawalRecord alipayWithdrawalRecord)
    {
        alipayWithdrawalRecord.setCreateTime(DateUtils.getNowDate());
        return alipayWithdrawalRecordMapper.insertAlipayWithdrawalRecord(alipayWithdrawalRecord);
    }

    /**
     * 修改提现记录
     * 
     * @param alipayWithdrawalRecord 提现记录
     * @return 结果
     */
    @Override
    public int updateAlipayWithdrawalRecord(AlipayWithdrawalRecord alipayWithdrawalRecord)
    {
        alipayWithdrawalRecord.setUpdateTime(DateUtils.getNowDate());
        return alipayWithdrawalRecordMapper.updateAlipayWithdrawalRecord(alipayWithdrawalRecord);
    }

    /**
     * 批量删除提现记录
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteAlipayWithdrawalRecordByIds(Long[] ids)
    {
        return alipayWithdrawalRecordMapper.deleteAlipayWithdrawalRecordByIds(ids);
    }

    /**
     * 删除提现记录信息
     * 
     * @param id 提现记录主键
     * @return 结果
     */
    @Override
    public int deleteAlipayWithdrawalRecordById(Long id)
    {
        return alipayWithdrawalRecordMapper.deleteAlipayWithdrawalRecordById(id);
    }

    /**
     * 申请提现
     * 
     * @param userId 用户ID
     * @param amount 提现金额
     * @return 提现记录ID
     */
    @Override
    public Long applyWithdrawal(String userId, BigDecimal amount)
    {
        AlipayWithdrawalRecord record = new AlipayWithdrawalRecord();
        record.setUserId(userId);
        record.setAmount(amount);
        record.setStatus(0); // 待处理状态
        record.setCreateTime(new Date());
        
        alipayWithdrawalRecordMapper.insertAlipayWithdrawalRecord(record);
        return record.getId();
    }

    /**
     * 处理提现申请
     * 
     * @param id 提现记录ID
     * @param status 处理状态(1:已完成,2:已拒绝)
     * @param remark 备注
     * @return 结果
     */
    @Override
    public int processWithdrawal(Long id, Integer status, String remark)
    {
        AlipayWithdrawalRecord record = new AlipayWithdrawalRecord();
        record.setId(id);
        record.setStatus(status);
        record.setRemark(remark);
        record.setProcessTime(new Date());
        
        return alipayWithdrawalRecordMapper.updateAlipayWithdrawalRecord(record);
    }
}